//
//  AYHomeViewController.swift
//  MyWeibo
//
//  Created by 昌俊 杨 on 29/9/2016.
//  Copyright © 2016 April. All rights reserved.
//

import UIKit

private let cellID = "cellID"

class AYHomeViewController: AYBaseViewController {

    var testData = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        naviItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        tableView?.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: cellID)
        
    }
    func showFriends()
    {
        print("friend list")
        let vc = AYDemoViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    override func loadData() {
         /** 延迟加载 */
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
            for i in 0..<15
            {
                var title = ""
                if (self.tbRefresh.isRefreshing)
                {
                    title = "上拉"
                    self.testData.insert(title + i.description, at:0)
                }
                else if (self.isPullUp)
                {
                    title = "下拉"
                    self.testData.append(title + i.description)
                }
                else
                {
                    self.testData.insert(title + i.description, at:0)
                }
                
            }
            self.tableView?.reloadData()
            self.tbRefresh.endRefreshing()
            self.isPullUp ? self.isPullUp = false : ()
        }
    }
    
}

// MARK: - tableView
extension AYHomeViewController
{

    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return testData.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
        cell.textLabel?.text = testData[indexPath.row]
        return cell
    }
}

